import com.proalias.particles.SpiralParticle;

/**
 * @author Alias Cummins
 */
class com.proalias.particles.Emitter extends MovieClip{
	public var t:Number;
	
	private var $particleNum:Number;
	private var $particles:Array;
	private var $maxParticles:Number;

	private var $pCount : Number;
	
	public function Emitter() {
		trace("Emitter.Emitter()");
	}
	
	public function init(maxParticles:Number) : Void {
		$particleNum = 0;
		$maxParticles = maxParticles;
		$pCount = 0;
		t = 0;
		$particles = new Array();
		onEnterFrame = step;
	}
	
	
	public function emit(particleNum:Number,color:Number) : Void {
		trace("Emitter.emit(particleNum, color)");
		if (particleNum + $particleNum > $maxParticles){
			particleNum = $maxParticles - $particleNum; 	
		}
		
		$particleNum += particleNum;		
		
		for (var p:Number = 0;p<particleNum;p++){
			$pCount+=1;
			var fp:SpiralParticle = SpiralParticle(attachMovie("SpiralParticle","particle"+$pCount,$pCount++));
			var scale:Number = Math.random()*800;
			fp._xscale = scale;
			fp._yscale = scale;
			
			fp._rotation = Math.random() * 360;
			var x:Number = Math.random()*100;
			var y:Number = Math.random()*100+200;
			var vx:Number = Math.random()*30; 
			var vy:Number = Math.random()*10; 
			 
			fp.init(x,y,vx,vy,this);
			
			$particles.push(fp);
				
		}
	}
	
	public function step() : Void {
		trace("Emitter.step()");
		t += 1;
		for (var i:Number = 0;i<$particles.length;i++){
			$particles[i].step();
			if ($particles[i]._y < -$particles[i]._height/2){
				$particles[i].die();
				$particleNum -=1;
				$particles.splice(i,1);
				emit(1);
			}
		}
		
	}
}